﻿$(function(){//延迟加载
	var gpId='';
	$('#grouplist').datagrid({
		url:'queryallgroup',
		columns:[[{
			field:'gpname',
			title:'分组名称',
			width:'40%',
			align:'center',
			styler: function () {
	            return 'background-color:#4CCBDE;color:white;font-weight:bold';
	         }				
		},{
			field:'gpdescribe',
			title:'职责描述',
			width:'55%',
		}]],
		toolbar: [{
			iconCls: 'icon-add',
			text:'添加分组',
			handler: function(){
				addgroup();
				}
		},'-',{
			iconCls: 'icon-remove',
			text:'删除分组',
			handler: function(){
				var row=$('#grouplist').datagrid('getSelected');
				if(row==''){
					return;
				}
				var r=confirm("确定删除?");
				if(r==true){
					$.ajax({
						url:'deletegroup',
						type:'POST',
						data:{
							gpId:row.gpId
						},
						success:function(data){
							alert(data.msg);
							$(this).datagrid('reload');
						}
					});
				}
			}
		},'-',{
			iconCls: 'icon-edit',
			text:'编辑分组',
			handler: function(){
				
			}
		},'-',{
			iconCls: 'icon-edit',
			text:'选人模式',
			handler: function(){
				parent.addTab('选人模式','toaddusertogroup');		
			}
		}],
		fit:true,
		striped:true,
		nowrap:true,
		pagination:true,
		rownumbers:true,
		autoRowHeight:false,
		singleSelect:true,
		onClickRow:function(index, row){
			$('#uglist').datagrid('reload',{
				gpId:row.gpId
			})
		}
	});
	$('#uglist').datagrid({
		url:'getgroupuserlist',
		param:{
			gpId:'',
		},
		columns:[[{
			field:'ck',
			checkbox:true
		},{
			field:'username',
			title:"员工姓名",
			width:'10%'
		},{
			field:'usertel',
			title:"手机号码",
			width:'20%'
		},{
			field:'dpname',
			title:"部门名称",
			width:'15%'
		},{
			field:'rootdpname',
			title:"隶属部门",
			width:'15%'
		},{
			field:'email',
			title:"邮箱",
			width:'20%'
		}]],
		toolbar: [{
			iconCls: 'icon-redo',
			text:'特权赋予',
			handler: function(){
//				addgroup();
				}
		},'-',{
			iconCls: 'icon-remove',
			text:'移出分组',
			handler: function(){
				var ugIds='';
				var rows=$('#uglist').datagrid('getChecked');
				gpId=$('#grouplist').datagrid('getSelected').gpId;
//				alert(gpId);
//				return;
				if(rows.length==0)
					return;
				for(var i=0;i<rows.length;i++){
					if(ugIds.length==0){
						ugIds=rows[i].ugId;
					}else{
						ugIds=ugIds+','+rows[i].ugId;
					}
				}
				$.ajax({
					url:'deleteuserfromgroup',
					type:'POST',
					data:{
						ugIds:ugIds,
					},
					success:function(data){
						alert(data.msg);
						$('#uglist').datagrid('reload',{
							gpId:gpId
						});
					}
				});
			}
		}],
		fit:true,
		striped:true,
		nowrap:true,
		pagination:true,
		rownumbers:true,
		autoRowHeight:false,
	});
	function addgroup(){		
		$('#addgroup').dialog({
			title:'添加新分组',
			width:'330px',
			height:'145px',
			modal: true
		});
		$('#addgroup').load('toaddnewgroup',function(){
			$.getScript('/sla/js/org/addgroup.js');
		});
	}
	
});